package com.jade.store.pay.service;

import com.jade.bss.DbException;
import com.jade.bss.base.RoleAction;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.store.pay.dao.WithDrawHisDao;
import com.jade.store.pay.entity.WithDrawHis;
import com.jade.store.pay.vo.WithDrawHisVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2016-4-17
 * Time: 13:45
 * Version : 1.0
 * Remark:
 * History:
 */
@Named("WithDrawHisHisFacade")
public class WithDrawHisFacadeImpl extends BaseFacadeImpl implements WithDrawHisFacade {

    @Inject
    @Named("WithDrawHisDao")
    private WithDrawHisDao entityDao;
    @Override
    @Transactional
    public void save(Object obj) throws DbException {
        WithDrawHis entity=(WithDrawHis)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        entity.setOwnerId(customer.getOwnerId());
        entity.setCreator(customer.getNickName());
        entity.setCreatorId(customer.getId());
        super.save(entity);
    }
    @Override
    @Transactional
    public void update(Object obj) throws DbException {
        WithDrawHis entity=(WithDrawHis)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        entity.setModifier(customer.getTrueName());
        super.update(entity);
    }

    @Override
    public PagingResult queryEntity(Object obj, int page, int size, String keyword, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        WithDrawHisVo entity=(WithDrawHisVo)obj;
        Criteria criteria = new Criteria((page - 1) * size, size);
        criteria.add(Restrictions.eq("ownerId",customer.getOwnerId()));
        if(StringUtils.isNotEmpty(keyword)){
            criteria.add(Restrictions.or(Restrictions.like("code", keyword),
                    Restrictions.like("name", keyword)));
        }
        criteria.addOrder(com.jade.framework.base.criterion.Order.desc(RoleAction.SORT_ITEM));
        PagingList pagingList = entityDao.queryEntity(WithDrawHis.class, criteria);
        PagingResult  result = PagingResult.getResult(CopyBean.copy(pagingList, WithDrawHisVo.class), pagingList.getTotalCount(), page, size);
        return result;
    }
}
